package com.indusfo.spc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.indusfo.spc.exception.ParamsErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.mapper.AssociTionMapper;
import com.indusfo.spc.pojo.AssociTion;
import com.indusfo.spc.service.AssocitionService;
import com.indusfo.spc.vo.JSONObject;

@Service
public class AsscoitionServiceImpl implements AssocitionService {
	private static final Logger logger = LoggerFactory.getLogger(AsscoitionServiceImpl.class);
	@Resource
	private AssociTionMapper associTionMapper;

	/**
	 * 根据角色的id查询角色数据
	 * 
	 * @see com.indusfo.spc.service.AssocitionService#selectAssociTion(com.indusfo.spc.pojo.AssociTion)
	 * @author hewb
	 * @date 2018年12月24日,下午6:10:54
	 */
	@Override
	public JSONObject selectAssociTion(AssociTion associTion) {
		List<AssociTion> asscoitionS = null;
		try {
			Integer lRoleId = associTion.getlRoleId();
			if (lRoleId == null) {
				throw new ModifyFailedException("角色Id不能为空");
			}
			asscoitionS = associTionMapper.selectAssociTion(lRoleId);
			if (asscoitionS.isEmpty()) {
				return JSONObject.oK("没有数据权限相关信息", asscoitionS, asscoitionS.size());
			}
			return JSONObject.oK("查询成功", asscoitionS, asscoitionS.size());
		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());
		}
	}

	/**
	 * 更新角色数据信息 (non-Javadoc)
	 * 
	 * @see com.indusfo.spc.service.AssocitionService#updateAssociTion(com.indusfo.spc.pojo.AssociTion)
	 * @author hewb
	 * @date 2018年12月24日,下午6:10:27
	 */
	@Override
	public JSONObject updateAssociTion(AssociTion associTion) {
		try {
			Integer lRoleId = associTion.getlRoleId();
			if (lRoleId == null) {
				throw new ModifyFailedException("角色Id不能为空");
			}

			int num = associTionMapper.updateByPrimaryKeySelective(associTion);
			if (num == 0) {
				throw new ModifyFailedException("更新数据失败");
			}
			return JSONObject.build(200, "更新数据成功", num);

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());
		}
	}

	/**
	 * 新增角色数据
	 * 
	 * @see com.indusfo.spc.service.AssocitionService#insertAssociTion(com.indusfo.spc.pojo.AssociTion)
	 * @author hewb
	 * @date 2018年12月24日,下午6:09:41
	 */
	@Override
	public JSONObject insertAssociTion(AssociTion associTion) {
		try {
			Integer lRoleId = associTion.getlRoleId();
			Integer lDepId = associTion.getlDepId();
			if (lDepId == null) {
				throw new ModifyFailedException("部门Id不能为空");
			}
			if (lRoleId == null) {
				throw new ModifyFailedException("角色Id不能为空");
			}
		 	int num = associTionMapper.insertSelective(associTion);
			if (num == 0) {
				throw new ModifyFailedException("新增数据失败");
			}
			return JSONObject.build(200, "新增数据成功", num);

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());
		}
	}

	/**
	 * 批量删除\启用和停用角色数据信息
	 * 
	 * @see com.indusfo.spc.service.AssocitionService#deleteAssociTion(java.lang.Long[],
	 *      java.lang.Integer, java.lang.Integer)
	 * @author hewb
	 * @date 2018年12月24日,下午6:11:41
	 */
	@Override
	public JSONObject deleteAssociTion(Long[] lDepId, Integer lRoleId, Integer lDataState) {
		try {
			if (lDepId == null) {
				throw new ModifyFailedException("部门Id不能为空");
			}
			if (lRoleId == null) {
				throw new ModifyFailedException("角色Id不能为空");
			}
			if (lDataState == null) {
				throw new ModifyFailedException("数据状态不能为空");
			}
			int num = associTionMapper.deleteAssociTion(lDepId, lRoleId, lDataState);
			String msg = "";
			if (num == 0) {
				switch (lDataState) {
				case 1:
					throw new ModifyFailedException("启用失败");
				case 2:
					throw new ModifyFailedException("删除失败");
				case 3:
					throw new ModifyFailedException("停用失败");
				}
			} else {
				switch (lDataState) {
				case 1:
					msg = "启用成功";
					break;
				case 2:
					msg = "删除成功";
					break;
				case 3:
					msg = "停用成功";
					break;
				}
			}
			return JSONObject.oK(msg);

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());
		}
	}
   /**
    * 将角色数据权限数据删除
    * @see com.indusfo.spc.service.AssocitionService#deleteAssociTionAll(java.lang.Integer)
    * @author hewb
    * @date 2018年12月26日,下午1:35:55
    */
	@Override
	public JSONObject deleteAssociTionAll(Integer lRoleId) {
		try {
			if (lRoleId == null) {
				throw new ModifyFailedException("角色id不能为空");
			}
			associTionMapper.deleteAssociTionAll(lRoleId);
			return JSONObject.oK();
		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());
		}
	}

	/**
	 * 根据角色ID删除数据权限
	 *
	 * @author xuz
	 * @date 2019/7/9 4:53 PM
	 * @param lRoleIds, lDataState]
	 * @return void
	 */
	@Override
	public void deleteAssociTionByRoleId(Long[] lRoleIds, Integer lDataState) {
		try {

			if (lRoleIds == null) {
				throw new ModifyFailedException("角色Id不能为空");
			}
			if (lDataState == null) {
				throw new ModifyFailedException("数据状态不能为空");
			}
			int num = associTionMapper.deleteAssociTionByRoleId(lRoleIds, lDataState);
			String msg = "";
			if (num == 0) {
				switch (lDataState) {
					case 1:
						throw new ModifyFailedException("启用失败");
					case 2:
						throw new ModifyFailedException("删除失败");
					case 3:
						throw new ModifyFailedException("停用失败");
				}
			} else {
				switch (lDataState) {
					case 1:
						msg = "启用成功";
						break;
					case 2:
						msg = "删除成功";
						break;
					case 3:
						msg = "停用成功";
						break;
				}
			}

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
		}
	}

	//---------------------------------- begin --------------------------------------//

	/**
	 * 根据角色ID查询数据权限集合
	 *
	 * @author xuz
	 * @date 2019/7/15 1:08 PM
	 * @param lRoleId
	 * @return java.util.List<com.indusfo.spc.pojo.AssociTion>
	 */
	@Override
	public List<AssociTion> queryAssocitionByRoleId(Integer lRoleId) {
		if (null==lRoleId) {
			throw new ParamsErrorException("角色ID不能为空");
		}
		List<AssociTion> associTionList = associTionMapper.selectAssociTion(lRoleId);
		return associTionList;
	}

	//---------------------------------- end --------------------------------------//
}
